/*
Joseph Ratway
Garrett Brown
Bret Parker
*/

//imports
import java.io.*;
import java.util.*;


public class Parser
{
	String command;
	Scanner user_input = new Scanner( System.in);
	int state = 0;
	

	Parser(){};

	int ParseLine(Board board)
	{
		System.out.print("Enter your Command or Move:");
		command = user_input.next();
		
		if(checkCommandSyntax(command)==true)
		{
			if(command.compareTo("REDO")==0)
			{
				board.RedoMove();
				return 3;
			}
			else if(command.compareTo("UNDO")==0)
			{
				board.UndoMove();
				return 4;
			}
			else if(command.compareTo("SHOW_NEXT_POS")==0)
			{
			
				board.isValidMove(board.currentBoard);
				board.printBoard();
				ParseLine(board);
				
				return 1;
				
			}
			else if(command.compareTo("EXIT")==0)
			{
			
				System.exit(0);
				return 1;
			}
			else if(command.compareTo("DISPLAY_ON")==0)
			{
				//display on
				
				return 1;
			}
			else if(command.compareTo("DISPLAY_OFF")==0)
			{
				//display off
				
				
				return 1;
			}
			else if(command.compareTo("EASY")==0)
			{
				if(board.player==1)
				{	while(board.endCondition>=0)
					{
							if(board.turnCount==0||(board.turnCount%2)==0)
							{
								ParseLine(board);
								//board.makeMove(board.currentBoard, board.player, command);
								board.turnCount++;
								System.out.println("TEST");
								board.printBoard();
								
							}
							if((board.turnCount%2)!=0)	
							{
								board.easy(board.currentBoard);
								board.turnCount++;
								board.printBoard();
								
								
							}
						
					}
				}
				return 1;
			}
			else if(command.compareTo("RANDOM")==0)
			{
				//Random
			
			
				return 1;
			}
			else if(command.compareTo("GREEDY")==0)
			{
				//easy
				
				
				return 1;
			}
			else if(command.compareTo("MEDIUM")==0)
			{
				//medium
				return 1;
			}
			else if(command.compareTo("HARD")==0)
			{
				//hard
				return 1;
			}
			else if(command.compareTo("BLACK")==0)
			{
				//black
				return 1;
			}
			else if(command.compareTo("WHITE")==0)
			{
				//white
				return 1;
			}
			else if(command.compareTo("ran")==0)
			{
				board.random(board.currentBoard);
				return 1;
			}
			else
			{
				return 0;
			}
		}
		
		else if(checkMoveSyntax(command)==true)
		{ 
			if(board.endCondition>=0)
			{
				board.makeMove(board.currentBoard, board.player, command);
				state=0;
			}
			return 1;
		}
		else
	    {
			System.out.println("Incorrect Input, Try Again");
			ParseLine(board);
		}
		
		return 0;
	}
	
	boolean checkCommandSyntax(String str)
	{	
	
		if( (str.compareTo("GREEDY")==0) ||(str.compareTo("RANDOM")==0) ||(str.compareTo("REDO")==0) ||(str.compareTo("UNDO")==0) || (str.compareTo("SHOW_NEXT_POS")==0) || (str.compareTo("EXIT")==0)|| (str.compareTo("DISPLAY_ON")==0) || (str.compareTo("DISPLAY_OFF")==0)|| (str.compareTo("EASY")==0) || (str.compareTo("MEDIUM")==0)|| (str.compareTo("HARD")==0) || (str.compareTo("BLACK")==0) || (str.compareTo("WHITE")==0)||(str.compareTo("ran")==0)) 
			return true;
		else
			return false;
				
	}
	
	boolean checkMoveSyntax(String str)
	{
	
		char[] charArray;
		charArray = str.toCharArray();
        if(str.length() < 2)
        {
            return false;
        }
	
		if((charArray[0] >= 'a' && charArray[0] <= 'z'))
		{
			if(Character.isDigit(charArray[1]) == true )
				return true;
			else
				return false;
		}
		else
			return false;	
	}
	
}
	
	
	
